package com.car.advertising.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.CarMsgEntiy;
import com.car.advertising.entity.bean.AutomobileBrandBean;
import com.car.advertising.view.BottomMenu;
import com.car.advertising.view.EditTextDeleteView;
import com.support.widget.JumpManager;
import com.support.widget.layout.BaseToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class CarOwnerBasicActivity extends BaseToolBarActivity {

    private TextView selectLicenseTv;
    private EditTextDeleteView licensePlateEdt;
    private TextView brandTv;
    private TextView commitTv;
    private RelativeLayout carBrandLl;
    private EditTextDeleteView driverEdt;
    private EditTextDeleteView drivingEdt;
    private AutomobileBrandBean brandBean;
    private String carNumber;
    private String jiashizheng;
    private String xingshizheng;

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("车辆基本信息");
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_car_owner_basic);

        selectLicenseTv = (TextView) findViewById(R.id.car_select_license_plate_tv);
        licensePlateEdt = (EditTextDeleteView) findViewById(R.id.car_license_plate_edt);
        driverEdt = (EditTextDeleteView) findViewById(R.id.car_driver_license_edt);
        drivingEdt = (EditTextDeleteView) findViewById(R.id.car_driving_license_edt);
        carBrandLl = (RelativeLayout) findViewById(R.id.car_brand_ll);
        brandTv = (TextView) findViewById(R.id.car_owner_brand_tv);
        commitTv = (TextView) findViewById(R.id.car_owner_commit_tv);

        selectLicenseTv.setText("沪");

        selectLicenseTv.setOnClickListener(this);
        carBrandLl.setOnClickListener(this);
        commitTv.setOnClickListener(this);
    }

    @Override
    public void onCritClick(View v) {
        switch (v.getId()) {
            case R.id.car_select_license_plate_tv:
                BottomMenu bottomMenu = new BottomMenu(CarOwnerBasicActivity.this, abbreviationListener);
                bottomMenu.show();
                break;
            case R.id.car_brand_ll:
                Bundle bundle = new Bundle();
                JumpManager.gotoActivityForResult(CarOwnerBasicActivity.this, AutomobileBrandActivity.class, false, bundle, 1);
                break;
            case R.id.car_owner_commit_tv:
                String selectLicense = selectLicenseTv.getText().toString();
                String licensePlateStr = licensePlateEdt.getText().toString().trim();
                String driverStr = driverEdt.getText().toString().trim();
                String drivingStr = drivingEdt.getText().toString().trim();
                if (TextUtils.isEmpty(licensePlateStr)) {
                    ToastUtil.show(CarOwnerBasicActivity.this, "请输入车牌号", Toast.LENGTH_SHORT);
                } else if (TextUtils.isEmpty(driverStr)) {
                    ToastUtil.show(CarOwnerBasicActivity.this, "请输入驾驶证编号", Toast.LENGTH_SHORT);
                } else if (TextUtils.isEmpty(drivingStr)) {
                    ToastUtil.show(CarOwnerBasicActivity.this, "请输入行驶证编号", Toast.LENGTH_SHORT);
                } else {
                    carNumber = selectLicense + licensePlateStr;
                    jiashizheng = driverStr;
                    xingshizheng = drivingStr;
                    getCarOwnerBasicData();
                }

                break;
            default:
                break;
        }
    }

    private BottomMenu.CityAbbreviationListener abbreviationListener = new BottomMenu.CityAbbreviationListener() {
        @Override
        public void onCityAbb(String abbreviation) {
            selectLicenseTv.setText(abbreviation);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            brandBean = data.getParcelableExtra("brandBean");
            brandTv.setText(brandBean.getBrandName());
        }
    }

    private void getCarOwnerBasicData() {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("acToken", CarApplication.token));
        params.add(new OkHttpClientManager.Param("driverLicenseImg", "d.png"));//驾驶证图片
        params.add(new OkHttpClientManager.Param("driverLicenseNum", jiashizheng));//驾驶证编号
        params.add(new OkHttpClientManager.Param("carLicenseNum", "f.png"));//行驶证图片
        params.add(new OkHttpClientManager.Param("carLicenseImg", xingshizheng));//行驶证编号
        params.add(new OkHttpClientManager.Param("carNum", carNumber));//车牌号
        params.add(new OkHttpClientManager.Param("carBrandId", "0"));//类型，0：轿车1：suv 2：面包车 3：其他
        if (brandBean != null) {
            params.add(new OkHttpClientManager.Param("carBrandName", brandBean.getBrandName()));//品牌名称，如雪弗莱等
            params.add(new OkHttpClientManager.Param("carType", brandBean.getId()));//品牌id，可调用getCarBrandList获取品牌信息
        } else {
            params.add(new OkHttpClientManager.Param("carBrandName", ""));//品牌名称，如雪弗莱等
            params.add(new OkHttpClientManager.Param("carType", ""));//品牌id，可调用getCarBrandList获取品牌信息
        }
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_035, new OkHttpClientManager.ResultCallback<CarMsgEntiy>() {
            @Override
            public void onError(String request) {
                ToastUtil.show(CarOwnerBasicActivity.this, "提交失败", Toast.LENGTH_SHORT);
            }

            @Override
            public void onResponse(CarMsgEntiy resp) {
                LogUtil.i(resp);
                ToastUtil.show(CarOwnerBasicActivity.this, "提交成功", Toast.LENGTH_SHORT);
                Contact.saveCarMsg(CarOwnerBasicActivity.this, resp);
                Intent intent = getIntent();
                setResult(2, intent);
                finish();
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }


}
